import request from '@/utils/request/index'
import type {
  UserResponseData,
  User,
  AllRoleResponseData,
  SetRoleData
} from './type'
enum API {
  ALLUSER_URL = '/admin/acl/user',
  ADDUSER_URL = '/admin/acl/user/save',
  UPDATEUSER_URL = '/admin/acl/user/update',
  ALLROLE_URL = '/admin/acl/user/toAssign',
  SETROLE_URL = '/admin/acl/user/doAssignRole',
  DELETEUSER_URL = '/admin/acl/user/remove',
  DELETEALLUSER_URL = '/admin/acl/user/batchRemove'
}

export const reqUserInfo = (page: number, limit: number, username: string) =>
  request.get<UserResponseData>(
    `${API.ALLUSER_URL}/${page}/${limit}?username=${username}`
  )

export const reqAddOrUpdateUser = (data: User) => {
  if (data.id) {
    return request.put<null>(`${API.UPDATEUSER_URL}`, data)
  } else {
    return request.post<null>(`${API.ADDUSER_URL}`, data)
  }
}

export const reqAllRole = (userId: number) =>
  request.get<AllRoleResponseData>(`${API.ALLROLE_URL}/${userId}`)

export const reqSetUserRole = (data: SetRoleData) =>
  request.post<null>(API.SETROLE_URL, data)

  export const reqRemoveUser = (userId: number) =>
    request.delete<null>(`${API.DELETEUSER_URL}/${userId}`)

export const reqSelectUser = (idList: number[]) =>
  request.delete<null>(API.DELETEALLUSER_URL, { data: idList })
